package com.example.sp04.srvice;

import com.example.sp04.feign.ItemClient;
import com.example.sp04.feign.UserClient;
import com.jt.sp01.pojo.Item;
import com.jt.sp01.pojo.Order;
import com.jt.sp01.pojo.User;
import com.jt.sp01.service.OrderService;
import com.jt.web.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Autowired
    private ItemClient itemClient;
    @Autowired
    private UserClient userClient;
    @Value("${server.port}")
    private Integer port;

    @Override
    public Order getOrder(String orderId) {
        log.info("获取订单,orderId="+orderId);
        //远程调用user-service获取用户信息
        JsonResult<User> user = userClient.getUser(8);
        //远程调用item-service获取商品信息
        JsonResult<List<Item>> items = itemClient.getItems(orderId);

        Order order = new Order();
        order.setId(orderId);
        order.setUser(user.getData());
        order.setItems(items.getData());

        return order;
    }


    @Override
    public void addOrder(Order order) {
        //远程调用item-service减少商品库存
        itemClient.decreaseNumber(order.getItems());
        //远程调用user-service增加用户积分
        userClient.addScroe(order.getUser().getId(),10000);
        log.info("保存订单："+order);
    }
}
